/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filmlibrary.web;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import filmlibrary.orm.*;
import filmlibrary.orm.classes.Films;
import filmlibrary.orm.classes.Genres;
import filmlibrary.orm.classes.Quality;
import java.util.Arrays;

import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author karol
 */
@ManagedBean
@ViewScoped
public class EditBean {

    private static final Logger logger = Logger.getLogger("filmlibrary.web.EditBean");
    private static int id;
    private ORMHelper orm = new ORMHelper();
    private String[] genrePl = {""};
    private Films film = null;
    private String quality = "";
    private String[] genreEn = {""};

    /** Creates a new instance of AddBean */
    public EditBean() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String Id = request.getParameter("id");

        if (Id != null ) {
            this.id = Integer.parseInt(Id);
        }
        if(this.id == 0)
        {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("myFilms.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(EditBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }        
        logger.info("konstrukor Edit id: " + id);
        film = orm.getFilmbyId((int) this.id);
        logger.info("konstruktor Edit film: " + film);
        logger.info(new SessionBean().getLogin()    );
        if(film ==null || !film.getUsers().getUsername().equals(new SessionBean().getLogin())){
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("404");
            } catch (IOException ex) {
                Logger.getLogger(EditBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }



        logger.info("konstruktor Edit film: " + film);
        quality = film.getQuality().getName();
        genrePl = new String[film.getGenreses().size()];
        genreEn = new String[film.getGenreses().size()];
        int i = 0;
        for (Genres g : film.getGenreses()) {
            genreEn[i] = g.getNameEn();
            genrePl[i] = g.getNamePl();
            ++i;
        }       
    }

    public String initEdit(int id) {
        
        
        

        return "edit.xhtml?faces-redirect=true";
    }

    public String edit() {
        if (FacesContext.getCurrentInstance().getViewRoot().getLocale().getLanguage() == "en") {
            orm.updateFilm(film, genreEn, quality, id);
        } else {
            orm.updateFilm(film, genrePl, quality, id);
        }

        return "myFilms.xhtml?faces-redirect=true";
    }

    public List<Genres> getGenresFromDB() {

        List<Genres> g = orm.getGenres();

        return g;
    }

    public List<Quality> getQualitiesFromDB() {

        List<Quality> q = orm.getQualities();

        return q;
    }

    public String[] getGenrePl() {
        return (String[]) genrePl.clone();
    }

    public String getGenrePlInString() {
        return Arrays.toString(genrePl);
    }

    public void setGenrePl(String genre[]) {
        this.genrePl = (String[]) genre.clone();
    }

    public String[] getGenreEn() {
        return (String[]) genreEn.clone();
    }

    public String getGenreEnInString() {
        return Arrays.toString(genreEn);
    }

    public void setGenreEn(String genre[]) {
        this.genreEn = (String[]) genre.clone();
    }

    public Films getFilm() {
        return film;
    }

    public void setFilm(Films film) {
        this.film = film;
    }

    public String getQuality() {
        return quality;
    }

    public void setQuality(String quality) {
        this.quality = quality;
    }

    public int getId() {
        return id;
    }
}
